﻿using System;
using System.Collections.Generic;


// The bulk of your plugin's code belongs in this file.
namespace UpDryTwistImageReader
{
    class PluginCode
    {
        // Instance variables stored ...
        const string csCONFIG = "configuration";
        const string csIMAGES = "images";
        const string csIMAGE = "image";
        const string csLASTUPDATE = "lastNewImage";
        const string csCOUNTDOWN = "refreshCountDown";

        // 'Update', 'Update2', and 'GetString' all return data back to Rainmeter, depending on
        // if the Rainmeter measure wants a numeric value or a string/text data.
        //
        // The 'Instance' member contains all of the data necessary to read the INI file
        // passed to your plugin when this instance was first initialized.  Remember: your plugin
        // may be initialized multiple times.  For example, a plugin that reads the free space
        // of a hard drive may be called multiple times -- once for each installed hard drive.

        public UInt32 Update(Rainmeter.Settings.InstanceSettings Instance)
        {
            return 0;
        }

        public double Update2(Rainmeter.Settings.InstanceSettings Instance)
        {
            return 0.0;
        }

        public string GetString(Rainmeter.Settings.InstanceSettings Instance)
        {
            Configuration config = (Configuration)Instance.GetTempValue(csCONFIG, null);
            MatchingImages matchingImages = (MatchingImages)Instance.GetTempValue(csIMAGES, null);
            string image = (string)Instance.GetTempValue(csIMAGE, null);
            DateTime lastNewImage = (DateTime)Instance.GetTempValue(csLASTUPDATE, DateTime.Now);
            int refreshCountDown = (int)Instance.GetTempValue(csCOUNTDOWN, 0);

            // config.log(((matchingImages == null) ? "NEW" : "REUSED") + "  CurrentImage = " + ((image == null) ? "null" : image) + " countdown = " + refreshCountDown.ToString() + " readAt = " + lastNewImage.ToString());

            if (config == null) {
                config = new Configuration(Instance);
                Instance.SetTempValue(csCONFIG, config);
            }

            // Check for updating the image.  Update the image if:
            // 1) The current image is unset
            // 2) There's a "countdown timer" (the UpdateDivider value from the config ... see QuotePlugin for details)
            // 3) There's a "UpdateSeconds" set in the config.
            bool refresh = true;
            if (image != null) {
                if (config.updateDivider > 0) {
                    if (refreshCountDown <= 0) {
                        refreshCountDown = config.updateDivider;
                    }
                    else {
                        refreshCountDown--;
                        refresh = false;
                    }
                    Instance.SetTempValue(csCOUNTDOWN, refreshCountDown);
                }
                else if (config.updateSeconds > 0) {

                    if (DateTime.Now.Subtract(lastNewImage).Seconds < config.updateSeconds) {
                        refresh = false;
                    }
                }
            }
            if (refresh) {
                Instance.SetTempValue(csLASTUPDATE, DateTime.Now);
                
                if (matchingImages == null) {
                    matchingImages = new MatchingImages(config);
                    Instance.SetTempValue(csIMAGES, matchingImages);
                }
                matchingImages.refreshImages();
                if (matchingImages.Count > 0) {

                    // Pick a random image from our array of matches.
                    image = matchingImages[new Random().Next(0, matchingImages.Count - 1)];
                    Instance.SetTempValue(csIMAGE, image);
                }
                else {
                    config.log("!!! No images found for UpDryTwistImageScanner ...");
                }
            }
            // Rainmeter.Log(Rainmeter.LogLevel.Debug, "UPDRYTWIST:  Returning image " + ((image==null)?"null":image));
            return image;
        }


        // 'ExecuteBang' is a way of Rainmeter telling your plugin to do something *right now*.
        // What it wants to do can be defined by the 'Command' parameter.
        public void ExecuteBang(Rainmeter.Settings.InstanceSettings Instance, string Command)
        {
            return;
        }
    }
}
